package com.cn.spjdbc.dao;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.cn.spjdbc.BaseDao;
import com.cn.spjdbc.ColumnNameTransform;
import com.cn.spjdbc.po.TokenMapper;
import com.cn.spjdbc.po.VUserToken;

@Repository
public class UserTokenDao extends BaseDao<Integer> {

	private DataSource dataSource;

	public int save() {
		String sql = "insert into v_user_token(uid,token,imei,device_name,create_ua,create_date) values(?,?,?,?,?,?)";
		return insert(sql, 1, "123", "123", "123", "123", new Date());
	}
	public void test() throws CannotGetJdbcConnectionException, SQLException{
		DatabaseMetaData dbMeta = getConnection().getMetaData();
		ResultSet tableSet = dbMeta.getTables(null, "%", "v_qbank_user", new String[]{"TABLE"});
		while(tableSet.next()){
			System.out.println(ColumnNameTransform.replaceUnderlineAndfirstToUpper(tableSet.getString("TABLE_NAME"), "_", ""));
		}
	}
	
	public VUserToken get(Integer id){
		String sql = "select * from v_user_token where pukey=?";
		//return getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper(VUserToken.class), id);
		return getJdbcTemplate().queryForObject(sql, new TokenMapper(), id);
	}
}
